package com.szd.qiongyou20.home.modle;

import android.content.Context;
import android.util.Log;

import com.szd.qiongyou20.home.bean.CityData;
import com.szd.qiongyou20.home.bean.HomeListData;
import com.szd.qiongyou20.home.bean.HomeNearData;
import com.szd.qiongyou20.home.bean.HotSearchBean;
import com.szd.qiongyou20.home.inteface.Interface_Data;
import com.szd.qiongyou20.utils.BaseUrl;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by SZD on 2016/12/12.
 * modle层的获取数据的接口实现类
 */

public class LoadHomeData implements LoadData {

    private Interface_Data mInterface_data;

    public LoadHomeData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        mInterface_data = retrofit.create(Interface_Data.class);
    }

    /**
     * 获取的是首页中的ViewPager中的数据
     * RecyclerView下半部分的数据
     * 点评条的数据
     * 导航条的搜索部分的数据
     *
     * @param context
     * @param loadHomeListData
     */
    @Override
    public void HomeListData(Context context, final LoadHomeListData loadHomeListData) {

        // 调用获取数据的方法
        Call<HomeListData> listData = mInterface_data.getListData();

        listData.enqueue(new Callback<HomeListData>() {
            @Override
            public void onResponse(Call<HomeListData> call, Response<HomeListData> response) {
                if (response.isSuccess()) {
                    //获取首页数据的整个对象
                    HomeListData homeListData = response.body();
                    loadHomeListData.loadDataSuccess(homeListData);
                } else {
                    loadHomeListData.loadDataFailed("加载失败!");
                }
            }

            @Override
            public void onFailure(Call<HomeListData> call, Throwable t) {
                loadHomeListData.loadDataFailed("网络访问失败!");
            }
        });
    }

    @Override
    public void HomeNearData(Context context, final LoadHomeNearData loadHomeNearData) {

        Call<HomeNearData> nearData = mInterface_data.getNearData();

        nearData.enqueue(new Callback<HomeNearData>() {
            @Override
            public void onResponse(Call<HomeNearData> call, Response<HomeNearData> response) {
                if (response.isSuccess()) {
                    HomeNearData homeNearData = response.body();
                    loadHomeNearData.loadDataSuccess(homeNearData);
                    loadHomeNearData.loadDataFailed("获取内容失败!");
                }
            }

            @Override
            public void onFailure(Call<HomeNearData> call, Throwable t) {
                loadHomeNearData.loadDataFailed("网络访问失败!");
            }
        });
    }

    @Override
    public void CityData(Context context, String city_id, final LoadCityData loadCityData) {
        Map<String, String> map = new HashMap<>();
        //?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&track_app_version=7.1.0&city_id=63
        map.put("client_id", "qyer_android");
        map.put("client_secret", "9fcaae8aefc4f9ac4915");
        map.put("track_app_version", "7.1.0");
        map.put("city_id", city_id);

        Log.i("==============", "CityData: " + city_id);

        Call<CityData> cityData = mInterface_data.getCityData(map);

        cityData.enqueue(new Callback<CityData>() {
            @Override
            public void onResponse(Call<CityData> call, Response<CityData> response) {
                if (response.isSuccess()) {
                    CityData cityData = response.body();
                    loadCityData.loadDataSuccess(cityData);
                } else {
                    loadCityData.loadDataFailed("获取数据失败!");
                }
            }

            @Override
            public void onFailure(Call<CityData> call, Throwable t) {
                loadCityData.loadDataFailed("网络连接失败!");
                Log.i(">>>>>>>>>>>>>", "onFailure: " + t.toString());
            }
        });
    }

    @Override
    public void Search(Context context, final LoadSearchData loadSearchData) {
        Call<HotSearchBean> searchData = mInterface_data.getSearchData();
        searchData.enqueue(new Callback<HotSearchBean>() {
            @Override
            public void onResponse(Call<HotSearchBean> call, Response<HotSearchBean> response) {
                if (response.isSuccess()) {
                    HotSearchBean searchBean = response.body();
                    loadSearchData.loadDataSuccess(searchBean);
                } else {
                    loadSearchData.loadDataFaile("加载数据失败!");
                }
            }

            @Override
            public void onFailure(Call<HotSearchBean> call, Throwable t) {
                Log.i("网络访问失败", "onFailure: " + t.toString());
            }
        });
    }


}
